package com.hyphenate.easeui.utils;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.hyphenate.easeui.EaseUI;
import com.hyphenate.easeui.EaseUI.EaseUserProfileProvider;
import com.hyphenate.easeui.R;
import com.hyphenate.easeui.domain.EaseUser;
import com.hyphenate.easeui.utils.map.BaseUrl;
import com.hyphenate.easeui.utils.map.JSONUtils;
import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.listener.ResponseListener;
import com.kongzue.baseokhttp.util.Parameter;

import java.util.Map;

public class EaseUserUtils {

    static EaseUserProfileProvider userProvider;

    static {
        userProvider = EaseUI.getInstance().getUserProfileProvider();
    }

    /**
     * get EaseUser according username
     *
     * @param username
     * @return
     */
    public static EaseUser getUserInfo(String username) {
        if (userProvider != null) {
            return userProvider.getUser(username);
        }

        return null;
    }

    /**
     * set user avatar
     *
     * @param username
     */
    public static void setUserAvatar(final Context context, String username, final ImageView imageView) {
        final EaseUser user = getUserInfo(username);
        HttpRequest.POST(context, BaseUrl.BaseHttpUrl + "Index/queryAccountByUUID"
                , new Parameter()
                        .add("token", Preferences.getInstance().getString(context, "user", "token"))
                        .add("uuid", username)
                , new ResponseListener() {
                    @Override
                    public void onResponse(String response, Exception error) {
                        if (error == null) {
                            Map<String, String> map = JSONUtils.parseKeyAndValueToMap(response);
                            if (map.get("code").equals("1")) {
                                Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
                                try {
                                    int avatarResId = Integer.parseInt(data.get("head_pic"));
                                    Glide.with(context).load(avatarResId).into(imageView);
                                } catch (Exception e) {
                                    //use default avatar
                                    Glide.with(context).load(data.get("head_pic"))
                                            .apply(RequestOptions.placeholderOf(R.drawable.ease_default_avatar)
                                                    .diskCacheStrategy(DiskCacheStrategy.ALL))
                                            .into(imageView);
                                }
                            }
                        }
                    }
                });
    }

    /**
     * set user's nickname
     */
    public static void setUserNick(final Context context, final String username, final TextView textView) {
        HttpRequest.POST(context, BaseUrl.BaseHttpUrl + "Index/queryAccountByUUID"
                , new Parameter()
                        .add("token", Preferences.getInstance().getString(context, "user", "token"))
                        .add("uuid", username)
                , new ResponseListener() {
                    @Override
                    public void onResponse(String response, Exception error) {
                        if (error == null) {
                            Map<String, String> map = JSONUtils.parseKeyAndValueToMap(response);
                            if (map.equals("1")) {
                                Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
                                if (textView != null) {
                                    textView.setText(data.get("nickname"));
                                }
                            }
                        }
                    }
                });
    }

}
